﻿#include "QCefStrResFilter.h"

//////////////////////////////////////////////////////////////////////////
//定义过滤类
QCefStrResFilter::QCefStrResFilter(QCefWidget * opr,
                                    const QString & strKey,
                                    const QString & strMethod,
                                    const QString & strUrl,
                                    int iResourceType)
    : m_operator(opr)
    , m_key(strKey)
    , m_method(strMethod)
    , m_url(strUrl)
    , m_resourceType(iResourceType)
{
    QCEF_DEBUG();
}

QCefStrResFilter::~QCefStrResFilter()
{
    QCEF_DEBUG();

    //数据接收完成，发出通知
    if (m_operator != nullptr)
    {
        m_operator->pageResponseFilterComplete(m_key, m_method, m_url, m_resourceType, m_data);
    }
}


bool QCefStrResFilter::InitFilter()
{
    QCEF_DEBUG();

    m_data.reserve(1024*1024);
    return true;
}

CefResponseFilter::FilterStatus QCefStrResFilter::Filter(void* data_in,
                            size_t data_in_size,
                            size_t& data_in_read,
                            void* data_out,
                            size_t data_out_size,
                            size_t& data_out_written)
{
    QCEF_DEBUG();

    // Write out the contents unchanged.
    data_out_written = std::min(data_in_size, data_out_size);
    if (data_out_written > 0)
    {
        memcpy(data_out, data_in, data_out_written);
    }

    data_in_read = data_out_written;

     //
    if (data_out_written > 0)
    {
        //缓存数据
        m_data.append((const char*)data_out, (int)data_out_written);
    }
    else
    {
        //一个文件分包传输时，Filter会被调用多次
        //数据接收完成，发出通知
        //if (m_operator != nullptr)
        //{
        //    m_operator->pageResponseFilterComplete(m_key, m_method, m_url, m_resourceType, m_data);
        //}

        //
        return RESPONSE_FILTER_DONE;
    }

    //
    return RESPONSE_FILTER_NEED_MORE_DATA;
}


